package com.bilubi.reggie.reggie.service.Impl;

import com.bilubi.reggie.reggie.annotation.AutoFill;
import com.bilubi.reggie.reggie.bean.Setmeal;
import com.bilubi.reggie.reggie.bean.SetmealDish;
import com.bilubi.reggie.reggie.bean.vo.SetmealVo;
import com.bilubi.reggie.reggie.mapper.SetmealDishMapper;
import com.bilubi.reggie.reggie.mapper.SetmealMapper;
import com.bilubi.reggie.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public List<SetmealVo> selectAll(String name) {
        return setmealMapper.selectAll(name);
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public Integer insertSetmeal(SetmealVo setmealVo) {
        setmealMapper.insertSelective(setmealVo);
        List<SetmealDish> setmealDishes = setmealVo.getSetmealDishes();
        Long setmealId = setmealVo.getId();
        setmealDishes.forEach(setmealDish -> {setmealDish.setSetmealId(String.valueOf(setmealId));});
        return setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public SetmealVo selectSetmealById(Long id) {
        return setmealMapper.selectSetmealVoWithDishesById(id);
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public Integer updateSetmeal(SetmealVo setmealVo) {
        setmealMapper.updateByPrimaryKeySelective(setmealVo);
        List<SetmealDish> setmealDishes = setmealVo.getSetmealDishes();
        Long setmealId = setmealVo.getId();
        // 更新套餐
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(String.valueOf(setmealId)));
        // 删除旧dish
        setmealDishMapper.deleteBySetmealId(setmealId);
        // 新增dish
        return setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public Integer updateStatusById(Integer status, String ids) {
        List<Setmeal> setmeals = new ArrayList<>();
        Arrays.stream(ids.split(",")).map(Long::parseLong).forEach(x->{
            Setmeal setmeal_ = new Setmeal();
            setmeal_.setId(x);
            setmeal_.setStatus(status);
            setmeals.add(setmeal_);
        });
        return setmealMapper.updateStatusById(setmeals);
    }

    @Override
    public Integer deleteById(String ids) {
        List<Setmeal> setmeals = new ArrayList<>();
        Arrays.stream(ids.split(",")).map(Long::parseLong).forEach(x->{
            Setmeal setmeal_ = new Setmeal();
            setmeal_.setId(x);
            setmeals.add(setmeal_);
        });
        return setmealMapper.deleteByPrimaryKey(setmeals);
    }

    @Override
    public List<Setmeal> selectSetmealByCategoryId(Long categoryId) {
        return setmealMapper.selectByCategoryId(categoryId);
    }
}
